package crl.menu.view
{
	import crl.menu.data.Item;
	import crl.menu.events.ListItemEvent;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class PageList extends Sprite
	{
		public var idx:int;
		public function PageList()
		{
			super();
			
			this.mouseEnabled=false;
		}
		
		
		public function display(list:Array,temp:DisplayObject):void{
			var len:int=0;
			
			if(list){
				len=list.length;
			}
			
			
			this.x=int(temp.x);
			this.y=int(temp.y);
			
			
			var p:DisplayObjectContainer=temp.parent;
			
			
			p.removeChild(temp);
			p.addChild(this);
			
			var nextPos:int=0;
			var item:ItemRender;
			for(var i:int=0;i<len;i++){
				item=new ItemRender();
				item.templete=temp;
				item.data=list[i] as Item;
				
				
				bindEvent(item);
				
				
				item.y=nextPos;
				
				this.addChild(item);
				
				nextPos+=item.height;
			}
			
		}
		
		
		private function bindEvent(item:ItemRender):void{
			item.addEventListener(MouseEvent.ROLL_OVER,mouseHandler);
			item.addEventListener(MouseEvent.ROLL_OVER,mouseHandler);
			item.addEventListener(MouseEvent.CLICK,mouseHandler);
		}
		
		
		private function mouseHandler(event:MouseEvent):void{
			
			var target:ItemRender=event.currentTarget as ItemRender;
			switch(event.type)
			{
				case MouseEvent.ROLL_OVER :
					
					this.dispatchEvent(new ListItemEvent(ListItemEvent.OVER,target,target.data));
					break;
				case MouseEvent.ROLL_OUT :
					
					this.dispatchEvent(new ListItemEvent(ListItemEvent.OVER,target,target.data));
					break;
				
				case MouseEvent.CLICK:
					this.dispatchEvent(new ListItemEvent(ListItemEvent.CLICK,target,target.data));
					break;
				
			}
		}
	}
}